﻿using Oracle.ManagedDataAccess.Client;
using Server.Constant;
using Server.Enum;
using Server.OptExp;
using Server.Framework.Message;
using Server.Framework.Processor;
using Server.Framework.Service;
using Server.Framework.Socket;
using Server.Message.Request;
using Server.Message.Response;
using Server.Model;
using System.Data.Common;
using System;

namespace Server.Processor
{
    /// <summary>
    /// Author: Leil
    /// Description: 新增或编辑角色操作
    /// CreateTime: 2020/5/13 10:12:49
    /// </summary>
    [ProcessorAttr(MessageId.ADD_EDIT_ROLE)]
    class AddEditRoleProcessor : IProcessor
    {

        private readonly string insertSql = @"INSERT INTO SYS_ROLE ( ID, ROLE_NAME, STATUS, CREATOR, CREATE_TIME ) VALUES ( seq_sys_role.NEXTVAL, :ROLE_NAME, :STATUS, :CREATOR, sysdate)";
        private readonly string updateSql = @"UPDATE SYS_ROLE SET ROLE_NAME = :ROLE_NAME, STATUS = :STATUS, UPDATOR = :UPDATOR, UPDATE_TIME = sysdate WHERE ID = :ID";

        public void Process(ProtobufAppSession session, RequestBaseMessage message)
        {
            if (session.User == null)
            {
                Console.WriteLine("用户未登录");
                return;
            }
            if (message is AddEditRoleReq msg)
            {
                OperationResp resp = new OperationResp(MessageId.OPT_RESP);
                if (msg.RoleDao == null)
                {
                    resp.Code = 500;
                    resp.OptType = (int)OptType.ADD_EDIT_ROLE;
                    resp.Content = "角色不能为空";
                    session.Send(resp);
                    return;
                } else
                {
                    try
                    {
                        if (msg.RoleDao.ID != 0)
                        {
                            EditRole(msg.RoleDao, session);
                        }
                        else
                        {
                            AddRole(msg.RoleDao, session);
                        }
                    }
                    catch (CommonOptException e)
                    {
                        resp.Code = 500;
                        resp.OptType = (int)OptType.ADD_EDIT_ROLE;
                        resp.Content = e.Message;
                        session.Send(resp);
                    }
                }
            }
        }


        /// <summary>
        /// 新增角色
        /// </summary>
        /// <param name="role">角色</param>
        /// <param name="resp">消息</param>
        private void AddRole(RoleDao role, ProtobufAppSession session)
        {
            if (string.IsNullOrEmpty(role.RoleName)) {
                throw new CommonOptException("角色名称不能为空");
            }
            DbParameter[] parameters = new DbParameter[] {
                new OracleParameter("ROLE_NAME", OracleDbType.Varchar2)
                {
                    Value = role.RoleName,
                },
                new OracleParameter("STATUS", OracleDbType.Int32)
                {
                    Value = role.Status,
                },
                new OracleParameter("CREATOR", OracleDbType.Varchar2)
                {
                    Value = session.User.LoginName,
                }
            };
            DBService.Instance().ExecuteNonQuery(insertSql, res => {
                OperationResp resp = new OperationResp(MessageId.OPT_RESP);
                if (res > 0)
                {
                    resp.Code = 200;
                    resp.OptType = (int)OptType.ADD_EDIT_ROLE;
                    resp.Content = "新增成功";
                    session.Send(resp);
                } else
                {
                    resp.Code = 500;
                    resp.OptType = (int)OptType.ADD_EDIT_ROLE;
                    resp.Content = "新增失败";
                    session.Send(resp);
                }
            }, parameters);
        }

        /// <summary>
        /// 编辑角色
        /// </summary>
        /// <param name="role">角色</param>
        /// <param name="resp">消息</param>
        private void EditRole(RoleDao role, ProtobufAppSession session)
        {
            if (string.IsNullOrEmpty(role.RoleName))
            {
                throw new CommonOptException("角色名称不能为空");
            }
            DbParameter[] parameters = new DbParameter[] {
                new OracleParameter("ROLE_NAME", OracleDbType.Varchar2)
                {
                    Value = role.RoleName,
                },
                new OracleParameter("STATUS", OracleDbType.Int32)
                {
                    Value = role.Status,
                },
                new OracleParameter("UPDATOR", OracleDbType.Varchar2)
                {
                    Value = session.User.LoginName,
                },
                new OracleParameter("ID", OracleDbType.Int32)
                {
                    Value = role.ID,
                }
            };
            DBService.Instance().ExecuteNonQuery(updateSql, res => {
                OperationResp resp = new OperationResp(MessageId.OPT_RESP);
                if (res > 0)
                {
                    resp.Code = 200;
                    resp.OptType = (int)OptType.ADD_EDIT_ROLE;
                    resp.Content = "更新成功";
                    session.Send(resp);
                }
                else
                {
                    resp.Code = 500;
                    resp.OptType = (int)OptType.ADD_EDIT_ROLE;
                    resp.Content = "更新失败";
                    session.Send(resp);
                }
            }, parameters);
        }


    }
}
